package com.atmc.controller;

import com.atmc.bean.Authority;
import com.atmc.bean.Bill;
import com.atmc.mapper.AuthorityMapper;
import com.atmc.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.List;
import java.util.Random;

@Controller
public class MyController {
    @Autowired
    BillService billService;
    @Autowired
    AuthorityMapper authorityMapper;

    @RequestMapping({"/","/index"})
    public String toIndex(Model model) {
//        Random random = new Random();
//        String result="";
//        for (int i=0;i<6;i++)
//        {
//            result+=random.nextInt(10);
//        }
////        System.out.println(result);
//        model.addAttribute("random","1223");
        return "login";
    }

    @RequestMapping("/LoginSuccess")
    public String toSuccess() {
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }
    @RequestMapping("/unauth")
    public String unauth() {
        return "unauth";
    }

    @RequestMapping("/html/one_set")
    public String toOneset(Model model) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        for (GrantedAuthority authority : authorities) {
            System.err.println(authority);
        }
        String name = authentication.getName();
        Authority authority = authorityMapper.selectOne(name);
        String role = authority.getRole();
        model.addAttribute("auth",authorities);
        model.addAttribute("name",name);
        model.addAttribute("role",role);
        return "html/one_set";
    }

    @RequestMapping("/html/welcome")
    public String toWelcome(Model model) {

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
//        System.err.println(name);
        model.addAttribute("userDetail",name);
        List<Bill> list = billService.selectAll();
        model.addAttribute("msg",list);
        return "html/welcome";
    }


    @RequestMapping("/html/sys-set")
    public String toSysset() {
        return "html/sys-set";
    }

    @RequestMapping("/html/sys-log")
    public String toSyslog() {
        return "html/sys-log";
    }

    @RequestMapping("/html/email")
    public String toEmail() {
        return "html/email";
    }
    @RequestMapping("/html/message")
    public String toBackups(){
        return "html/message";
    }




}